﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.BLL;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.Utils;
using DevExpress.XtraGrid;
using DevExpress.XtraPrinting;
using FlyHorse.LIMS.WinApp.XtraReportTemplate;
using DevExpress.XtraReports.UI;
using DevExpress.LookAndFeel;
using FlyHorse.LIMS.WinApp.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;
using System.Collections;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Data;
using DevExpress.XtraGrid.Menu;
using DevExpress.Utils.Menu;
using System.Drawing.Printing;

namespace FlyHorse.LIMS.WinApp.DataManageForms.ReportSemiForms
{
    public partial class ReportSemiMainForm : DevExpress.XtraEditors.XtraForm
    {
        ToolBll toolBll = new ToolBll();
        BLL.ReportSemiBll reportSemiBll = new ReportSemiBll();
        IQueryable<ReportSemi> _models;
       // private double? Value_Column;
       // private double Value_Group;
        Action<ReportSemi> _FillData;
        public ReportSemiMainForm()
        {
            InitializeComponent();
            gridView1.OptionsBehavior.AllowPartialGroups = DevExpress.Utils.DefaultBoolean.True;
            gridView1.OptionsView.ShowGroupExpandCollapseButtons = true;
            gridView1.OptionsBehavior.AutoExpandAllGroups = true;
            gridView1.OptionsBehavior.KeepGroupExpandedOnSorting = true;

            ribbonPageGroup4.Visible = false;
        }


        /// <summary>
        /// 出厂报告填充数据的时候条用此构造方法
        /// </summary>
        public ReportSemiMainForm(Action<ReportSemi> FillData)
        {
            InitializeComponent();
            gridView1.OptionsBehavior.AllowPartialGroups = DevExpress.Utils.DefaultBoolean.True;
            gridView1.OptionsView.ShowGroupExpandCollapseButtons = true;
            gridView1.OptionsBehavior.AutoExpandAllGroups = true;
            gridView1.OptionsBehavior.KeepGroupExpandedOnSorting = true;

            ribbonPageGroup1.Visible = false;
            _FillData = FillData;
        }



        private void ReportSemiMainForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;

            CheckAuthority();

            //绑定grid里的样品名称
            List<Good> goodsAllList = toolBll.GetAllGoodList();
            repositoryItemLookUpEdit1.DataSource = goodsAllList;
            ////绑定grid里的取样位置
            repositoryItemLookUpEdit2.DataSource = toolBll.GetAllSamplingPlaceList();

            ////绑定grid里的生产线
            repositoryItemLookUpEdit3.DataSource = toolBll.GetProductionLineList();
            ////绑定grid里的班组
            repositoryItemLookUpEdit4.DataSource = toolBll.GetProductionGropList();
            ////绑定grid里的录入人
            repositoryItemLookUpEdit5.DataSource = toolBll.GetAllLoginUersList();
            repositoryItemLookUpEdit10.DataSource = toolBll.GetTestItemList();

            //绑定搜索条件里的样品名称下拉框
            repositoryItemLookUpEdit6.DataSource = toolBll.GetSemiGoodList();
            //绑定取样位置-搜索条件
            repositoryItemLookUpEdit7.DataSource = toolBll.GetSamplingPlaceList().OrderBy(m=>m.SamplingPlaceName);
            repositoryItemLookUpEdit8.DataSource = toolBll.GetProductionGropList();   //班组
            repositoryItemLookUpEdit9.DataSource = toolBll.GetProductionLineList();  //生产线

            var models = reportSemiBll.LoadEntitiesAsNoTracking(m => true);
            _models = models;
            InitGrid(_models);
            //gridView2.BeginDataUpdate();
            //try
            //{
            //    gridView2.ClearSorting();
            //    gridView2.Columns["FormOrder"].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            //}
            //finally
            //{
            //    gridView2.EndDataUpdate();
            //}
        }

        private void InitGrid(IQueryable<ReportSemi> models)
        {
            models = models.OrderByDescending(m => m.SendDate).ThenBy(m => m.GoodsID);
            int pageIndex = Convert.ToInt32(this.barStaticItem_pageIndex.Caption);
            int pageSize = 0;
            try
            {
                pageSize = Convert.ToInt32(this.barEditItem_pageSize.EditValue);
            }
            catch (Exception)
            {
                XtraMessageBox.Show(ResourceOverall.TotalPageErr);
                return;
            }
            if (pageSize == 0)
            {
                barEditItem_pageSize.EditValue = 30;
                pageSize = 30;
            }
            splashScreenManager1.ShowWaitForm();
            //...?
           


            PagedList<ReportSemi> pagedList = new PagedList<ReportSemi>(models, pageIndex, pageSize);
            this.barStaticItem_pageIndex.Caption = pagedList.PageIndex.ToString();
            this.barStaticItem_totalPage.Caption = pagedList.TotalPages.ToString();
            this.barEditItem_pageSize.Caption = pagedList.PageSize.ToString();

            this.gridControl1.DataSource = TransformReport.TransformSemiReport(pagedList);

            //gridView2.BeginDataUpdate();
            //try
            //{
            //    gridView2.Columns["FormOrder"].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            //}
            //finally
            //{
            //    gridView2.EndDataUpdate();
            //}

            GridColumn colReceived = gridView1.Columns["SendDate"];
            gridView1.BeginSort();
            try
            {
                gridView1.ClearGrouping();
                colReceived.GroupIndex = 0;
            }
            finally
            {
                gridView1.EndSort();
            }

            splashScreenManager1.CloseWaitForm();
        }

        private void ButtonItem_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ReportSemiDetailForm reportSemiDetailForm = new ReportSemiDetailForm(ShowAllData);
            reportSemiDetailForm.ShowDialog();

        }

        private void ButtonItem_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenEditForm();
        }

        private void OpenEditForm()
        {
            if (this.gridView1.GetFocusedRowCellValue("ReportSemiID") != null)
            {
                string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("ReportSemiID"));
                ReportSemi report = reportSemiBll.LoadEntities(m => m.ReportSemiID == id).FirstOrDefault();
                if (report != null)
                {
                    _models = reportSemiBll.LoadEntities(m => true);

                    ReportSemiDetailForm reportSemiDetailForm = new ReportSemiDetailForm(report, _models, InitGrid);
                    reportSemiDetailForm.ShowDialog();
                }
            }

        }

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("ReportSemiID") != null)
            {
                DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("ReportSemiID"));
                    
                    using (OJCB4Entities db =new OJCB4Entities())
                    {
                        ReportSemi report = db.ReportSemis.Find(id);
                        db.ReportSemis.Remove(report);
                        bool res = db.SaveChanges() > 0;
                        if (res)
                        {
                            InitGrid(_models);
                            XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                        }
                        else
                        {
                            XtraMessageBox.Show(ResourceOverall.DeleteFailed);
                        }
                    }
                }
            }



        }

        private void ShowAllData()
        {
            this.barEditItem_GoodsName.EditValue = null;
            this.barEditItem_place.EditValue = null;
            this.barEditItem_startDate.EditValue = null;
            this.barEditItem_endDate.EditValue = null;
            barEditItem_Group.EditValue = null;
            barEditItem_Line.EditValue = null;

            _models = reportSemiBll.LoadEntities(m => true);
            InitGrid(_models);
        }

        private void barButton_firstPage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.barStaticItem_pageIndex.Caption = "1";
            InitGrid(_models);
        }

        private void barButton_previousPage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int pageIndex = Convert.ToInt32(this.barStaticItem_pageIndex.Caption);
            if (pageIndex >= 1)
            {
                this.barStaticItem_pageIndex.Caption = (pageIndex - 1).ToString();
                InitGrid(_models);
            }
        }

        private void barButton_nextPage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int pageIndex = Convert.ToInt32(this.barStaticItem_pageIndex.Caption);
            int totalPage = Convert.ToInt32(barStaticItem_totalPage.Caption);
            if (pageIndex < totalPage)
            {
                this.barStaticItem_pageIndex.Caption = (pageIndex + 1).ToString();
                InitGrid(_models);
            }
        }

        private void barButton_lastPage_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int pageIndex = Convert.ToInt32(this.barStaticItem_pageIndex.Caption);
            int totalPage = Convert.ToInt32(barStaticItem_totalPage.Caption);
            if (pageIndex < totalPage)
            {
                this.barStaticItem_pageIndex.Caption = totalPage.ToString();
                InitGrid(_models);
            }
        }

        private void barEditItem_pageSize_EditValueChanged(object sender, EventArgs e)
        {
            InitGrid(_models);
        }

        private void ButtonItem_Print_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //ShowGridPreview(this.gridControl1);
            //PrintingSystem print = new DevExpress.XtraPrinting.PrintingSystem();
            //PrintableComponentLink link = new PrintableComponentLink(print);
            //print.Links.Add(link);
            //link.Component = this.gridControl1;//这里可以是可打印的部件
            //string _PrintHeader = "新增会员统计";
            //PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter;
            //phf.Header.Content.Clear();
            //phf.Header.Content.AddRange(new string[] { "", _PrintHeader, "" });
            //phf.Header.Font = new System.Drawing.Font("宋体", 14, System.Drawing.FontStyle.Bold);
            //phf.Header.LineAlignment = BrickAlignment.Center;
            //link.CreateDocument(); //建立文档
            //print.PreviewFormEx.ShowDialog();//进行预览

            object dataSource = this.gridControl1.DataSource;
            List<ReportSemiModel> reportList = (List<ReportSemiModel>)dataSource;

            XtraReportSemi report = new XtraReportSemi(reportList);
            using (ReportPrintTool printTool = new ReportPrintTool(report))
            {
                // Invoke the Ribbon Print Preview form modally, 
                // and load the report document into it.
                printTool.ShowRibbonPreviewDialog();
                // Invoke the Ribbon Print Preview form
                // with the specified look and feel setting.
                printTool.ShowRibbonPreview(UserLookAndFeel.Default);
            }
        }

        private void ShowGridPreview(GridControl grid)
        {
            // Check whether the GridControl can be previewed.
            if (!grid.IsPrintingAvailable)
            {
                MessageBox.Show("The 'DevExpress.XtraPrinting' library is not found", "Error");
                return;
            }
            // Open the Preview window.
            grid.ShowPrintPreview();
        }

        private void PrintGrid(GridControl grid)
        {
            // Check whether the GridControl can be printed.
            if (!grid.IsPrintingAvailable)
            {
                MessageBox.Show("The 'DevExpress.XtraPrinting' library is not found", "Error");
                return;
            }

            // Print.
            grid.Print();
        }

        private void CheckAuthority()
        {
            List<Resource> resList = LoginUserUtil.LoginUser.Role.Resources.ToList();
            if (resList.Where(m => m.ResourceKey == "SemiReportButtonItem_Add").Count() != 1)
            {
                ButtonItem_Add.Enabled = false;
            }
            if (resList.Where(m => m.ResourceKey == "SemiReportButtonItem_Edit").Count() != 1)
            {
                ButtonItem_Edit.Enabled = false;
            }
            if (resList.Where(m => m.ResourceKey == "SemiReportButtonItem_Delete").Count() != 1)
            {
                ButtonItem_Delete.Enabled = false;
            }
            if (resList.Where(m => m.ResourceKey == "SemiReportButtonItem_Print").Count() != 1)
            {
                ButtonItem_Print.Enabled = false;
            }
            if (resList.Where(m => m.ResourceKey == "SemiReportButtonItem_PrintAll").Count() != 1)
            {
                ButtonItem_PrintAll.Enabled = false;
            }
        }

        private void ButtonItem_PrintLandscape_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            object dataSource = this.gridControl1.DataSource;
            List<ReportSemiModel> reportList = (List<ReportSemiModel>)dataSource;

            XtraReportSemi report = new XtraReportSemi(reportList);
            using (ReportPrintTool printTool = new ReportPrintTool(report))
            {
                // Invoke the Ribbon Print Preview form modally, 
                // and load the report document into it.
                printTool.ShowRibbonPreviewDialog();

                // Invoke the Ribbon Print Preview form
                // with the specified look and feel setting.
                printTool.ShowRibbonPreview(UserLookAndFeel.Default);
            }
        }

        private void ribbonGalleryBarItem1_GalleryItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)
        {
            if ((string)e.Item.Value == "U1")
            {
                _models = this.reportSemiBll.LoadEntitiesAsNoTracking(m => m.ProductionLineID == "1");
                InitGrid(_models);
            }
            else if ((string)e.Item.Value == "U2")
            {
                _models = this.reportSemiBll.LoadEntitiesAsNoTracking(m => m.ProductionLineID == "2");
                InitGrid(_models);
            }
            else if ((string)e.Item.Value == "U1Mix")
            {
                _models = this.reportSemiBll.LoadEntitiesAsNoTracking(m => m.ProductionLineID == "1" && m.SamplingPlaceID == "2");
                InitGrid(_models);
            }
            else if ((string)e.Item.Value == "U2Mix")
            {
                _models = this.reportSemiBll.LoadEntitiesAsNoTracking(m => m.ProductionLineID == "2" && m.SamplingPlaceID == "2");
                InitGrid(_models);
            }
            else if ((string)e.Item.Value == "U1DO")
            {
                _models = this.reportSemiBll.LoadEntitiesAsNoTracking(m => m.ProductionLineID == "1" && m.SamplingPlaceID == "5");
                InitGrid(_models);
            }
            else if ((string)e.Item.Value == "U2DO")
            {
                _models = this.reportSemiBll.LoadEntitiesAsNoTracking(m => m.ProductionLineID == "2" && m.SamplingPlaceID == "5");
                InitGrid(_models);
            }
            else if ((string)e.Item.Value == "U1PO")
            {
                _models = this.reportSemiBll.LoadEntitiesAsNoTracking(m => m.ProductionLineID == "1" && m.SamplingPlaceID == "6");
                InitGrid(_models);
            }
            else if ((string)e.Item.Value == "U2PO")
            {
                _models = this.reportSemiBll.LoadEntitiesAsNoTracking(m => m.ProductionLineID == "2" && m.SamplingPlaceID == "6");
                InitGrid(_models);
            }

        }

        private void gridView1_CustomColumnGroup(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e)
        {
            if (e.Column.FieldName == "SendDate")
            {
                e.Handled = true;
                string val1 = Convert.ToDateTime(e.Value1).ToShortDateString();
                string val2 = Convert.ToDateTime(e.Value2).ToShortDateString();
                if (val1 == val2)
                    e.Result = 0;
                else e.Result = System.Collections.Comparer.Default.Compare(val1, val2);
            }

        }

        private void barButtonItem_Search_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var models = reportSemiBll.LoadEntitiesAsNoTracking(m => true);
            if (barEditItem_GoodsName.EditValue != null)
            {
                string goddsID = Convert.ToString(this.barEditItem_GoodsName.EditValue);
                models = models.Where(m => m.GoodsID == goddsID);
            }
            if (this.barEditItem_startDate.EditValue != null)
            {
                DateTime dateTime = Convert.ToDateTime(this.barEditItem_startDate.EditValue);
                models = models.Where(m => m.SendDate >= dateTime);
            }
            if (this.barEditItem_endDate.EditValue != null)
            {
                DateTime datetimeEnd = Convert.ToDateTime(this.barEditItem_endDate.EditValue);
                models = models.Where(m => m.SendDate <= datetimeEnd);
            }
            if (this.barEditItem_place.EditValue != null)
            {
                string placeID = Convert.ToString(this.barEditItem_place.EditValue);
                models = models.Where(m => m.SamplingPlaceID == placeID);
            }
            if (this.barEditItem_Group.EditValue != null)
            {
                string groupID = Convert.ToString(this.barEditItem_Group.EditValue);
                models = models.Where(m => m.ProductionGroupID == groupID);
            }
            if (this.barEditItem_Line.EditValue != null)
            {
                string lineID = Convert.ToString(this.barEditItem_Line.EditValue);
                models = models.Where(m => m.ProductionLineID == lineID);
            }

            _models = models;
            InitGrid(_models);
        }

        private void barButtonItem_Clear_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowAllData();
        }

        private void ButtonItem_PrintAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            PreviewPrintableComponent(gridControl1, gridControl1.LookAndFeel);
        }

        private void PreviewPrintableComponent(IPrintable component, UserLookAndFeel lookAndFeel)
        {
            // Create a link that will print a control.?
            PrintableComponentLink link = new PrintableComponentLink()
            {
                PrintingSystemBase = new PrintingSystemBase(),
                Component = component,
                Landscape = true,
                PaperKind = PaperKind.A4,
                //Margins = new Margins(20, 20, 20, 20)
            };
            // Show the report.?
            link.ShowRibbonPreview(lookAndFeel);
        }

        private void barButtonItem_FillReport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("ReportSemiID") != null)
            {
                string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("ReportSemiID"));
                ReportSemi report = reportSemiBll.LoadEntities(m => m.ReportSemiID == id).FirstOrDefault();
                if (report != null)
                {
                    _FillData(report);
                    this.Close();
                }

            }
        }


        #region 废弃代码    


        //private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        //{
        //    if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Summary)
        //    {
        //        GridViewFooterMenu menu = e.Menu as GridViewFooterMenu;
        //        GridView gridView1 = sender as GridView;
        //        //Erasing the default menu items?
        //        menu.Items.Clear();
        //        if (menu.Column != null)
        //        {
        //            DXMenuCheckItem checkItemCount = new DXMenuCheckItem("数量(Count)");
        //            checkItemCount.Image = imageList1.Images[3];
        //            checkItemCount.CheckedChanged += OnMinClick;
        //            checkItemCount.Tag = new MenuInfo(menu.Column, "Count", gridView1, "数量(Count)={0}");

        //            DXMenuCheckItem checkItemMin = new DXMenuCheckItem("最小值(Min)");
        //            checkItemMin.Image = imageList1.Images[0];
        //            checkItemMin.CheckedChanged += OnMinClick;
        //            checkItemMin.Tag = new MenuInfo(menu.Column, "Min", gridView1, "最小值(Min)={0}");

        //            DXMenuCheckItem checkItemMax = new DXMenuCheckItem("最大值(Max)");
        //            checkItemMax.Image = imageList1.Images[1];
        //            checkItemMax.CheckedChanged += OnMinClick;
        //            checkItemMax.Tag = new MenuInfo(menu.Column, "Max", gridView1, "最大值(Max)={0}");

        //            DXMenuCheckItem checkItemMedian = new DXMenuCheckItem("中位数(Median)");
        //            checkItemMedian.Image = imageList1.Images[2];
        //            checkItemMedian.CheckedChanged += OnMinClick;
        //            checkItemMedian.Tag = new MenuInfo(menu.Column, "Median", gridView1, "中位数(Median)={0}");

        //            DXMenuCheckItem checkItemSum = new DXMenuCheckItem("和(Sum)");
        //            checkItemSum.Image = imageList1.Images[3];
        //            checkItemSum.CheckedChanged += OnMinClick;
        //            checkItemSum.Tag = new MenuInfo(menu.Column, "Sum", gridView1, "和(Sum)={0}");

        //            DXMenuCheckItem checkItemAvg = new DXMenuCheckItem("平均值(Avg)");
        //            checkItemAvg.Image = imageList1.Images[4];
        //            checkItemAvg.CheckedChanged += OnMinClick;
        //            checkItemAvg.Tag = new MenuInfo(menu.Column, "Avg", gridView1, "平均值(Avg)={0}");

        //            DXMenuCheckItem checkItemR = new DXMenuCheckItem("全距(Range)");
        //            checkItemR.Image = imageList1.Images[5];
        //            checkItemR.CheckedChanged += OnMinClick;
        //            checkItemR.Tag = new MenuInfo(menu.Column, "Range", gridView1, "全距(Range)={0}");

        //            DXMenuCheckItem checkItemSigma = new DXMenuCheckItem("总体标准偏差(Sigma)");
        //            checkItemSigma.Image = imageList1.Images[6];
        //            checkItemSigma.CheckedChanged += OnMinClick;
        //            checkItemSigma.Tag = new MenuInfo(menu.Column, "Sigma", gridView1, "总体标准偏差(Sigma)={0}");

        //            DXMenuCheckItem checkItemSigmaS = new DXMenuCheckItem("样本标准偏差(S)");
        //            checkItemSigmaS.Image = imageList1.Images[7];
        //            checkItemSigmaS.CheckedChanged += OnMinClick;
        //            checkItemSigmaS.Tag = new MenuInfo(menu.Column, "S", gridView1, "样本标准偏差(S)={0}");



        //            DXMenuCheckItem checkItemSigmaCP = new DXMenuCheckItem("精密度(CP)");
        //            checkItemSigmaCP.Image = imageList1.Images[8];
        //            checkItemSigmaCP.CheckedChanged += OnMinClick;
        //            checkItemSigmaCP.Tag = new MenuInfo(menu.Column, "CP", gridView1, "精密度(CP)={0}");

        //            DXMenuCheckItem checkItemSigmaCA = new DXMenuCheckItem("准确度(CA)");             //需要双边
        //            checkItemSigmaCA.Image = imageList1.Images[9];
        //            checkItemSigmaCA.CheckedChanged += OnMinClick;
        //            checkItemSigmaCA.Tag = new MenuInfo(menu.Column, "CA", gridView1, "准确度(CA)={0}");

        //            DXMenuCheckItem checkItemSigmaCPK = new DXMenuCheckItem("制程能力指数(CPK)");     //需要双边
        //            checkItemSigmaCPK.Image = imageList1.Images[10];
        //            checkItemSigmaCPK.CheckedChanged += OnMinClick;
        //            checkItemSigmaCPK.Tag = new MenuInfo(menu.Column, "CPK", gridView1, "制程能力指数(CPK)={0}");

        //            DXMenuCheckItem checkItemSigmaClear = new DXMenuCheckItem("清空(Clear)");
        //            checkItemSigmaClear.Image = imageList1.Images[11];
        //            checkItemSigmaClear.CheckedChanged += OnMinClick;
        //            checkItemSigmaClear.Tag = new MenuInfo(menu.Column, "Clear", gridView1, "清空");


        //            menu.Items.Add(checkItemCount);
        //            menu.Items.Add(checkItemMin);
        //            menu.Items.Add(checkItemMax);
        //            menu.Items.Add(checkItemMedian);
        //            menu.Items.Add(checkItemSum);
        //            menu.Items.Add(checkItemAvg);
        //            menu.Items.Add(checkItemR);
        //            menu.Items.Add(checkItemSigmaS);
        //            menu.Items.Add(checkItemSigmaCP);
        //            menu.Items.Add(checkItemSigmaCA);
        //            menu.Items.Add(checkItemSigmaCPK);
        //            menu.Items.Add(checkItemSigmaClear);
        //        }
        //    }


        //}

        //void OnMinClick(object sender, EventArgs e)    //EventArgs
        //{
        //    DXMenuItem item = sender as DXMenuItem;
        //    MenuInfo menuInfo = (MenuInfo)item.Tag;
        //    GridColumn column = menuInfo.Column;
        //    string key = menuInfo.Key;
        //    string displayFormat = menuInfo.DisplayFormat;
        //    if (key!="Clear")
        //    {
        //        GridColumnSummaryItem summaryItemColMin = new GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, column.FieldName, displayFormat);
        //        summaryItemColMin.Tag = menuInfo;
        //        column.Summary.Add(summaryItemColMin);
        //    }
        //    else
        //    {
        //        column.Summary.Clear();
        //    }
        //}


        //private void gridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
        //{
        //    GridView view = sender as GridView;
        //    // Get the summary ID. ?
        //    //string summaryID = Convert.ToString((e.Item as GridSummaryItem).Tag);
        //    MenuInfo menuInfo = (MenuInfo)(e.Item as GridSummaryItem).Tag;
        //    GridColumn column = menuInfo.Column;
        //    string key = menuInfo.Key;
        //    // Initialization. ?
        //    if (e.SummaryProcess == CustomSummaryProcess.Start)
        //    {
        //        Value_Column = null;
        //    }
        //    #region 废弃代码
        //    // Calculation.?
        //    //if (e.SummaryProcess == CustomSummaryProcess.Calculate)
        //    //{
        //    //    object value = view.GetRowCellValue(e.RowHandle, column.FieldName);
        //    //    switch (key)
        //    //    {
        //    //        case "Min":
        //    //            if (value != null)
        //    //            {
        //    //                double temp = Convert.ToDouble(value);
        //    //                if (Value_Column == null)
        //    //                {
        //    //                    Value_Column = temp;
        //    //                }
        //    //                else
        //    //                {
        //    //                    if (temp < Value_Column)
        //    //                    {
        //    //                        Value_Column = temp;
        //    //                    }
        //    //                }
        //    //            }
        //    //            break;
        //    //        case "Max":
        //    //            if (value != null)
        //    //            {
        //    //                double temp = Convert.ToDouble(value);
        //    //                if (Value_Column == null)
        //    //                {
        //    //                    Value_Column = temp;
        //    //                }
        //    //                else
        //    //                {
        //    //                    if (temp > Value_Column)
        //    //                    {
        //    //                        Value_Column = temp;
        //    //                    }
        //    //                }
        //    //            }
        //    //            break;
        //    //        //case "Median":
        //    //        //    List<double> dataList = new List<double>();




        //    //        //    if (value != null)
        //    //        //    {
        //    //        //        double temp = Convert.ToDouble(value);
        //    //        //        dataList.Add(temp);
        //    //        //    }
        //    //        //    if (dataList.Count > 1)
        //    //        //    {
        //    //        //        dataList = dataList.OrderBy(m => m).ToList();
        //    //        //        //奇数
        //    //        //        int count = dataList.Count;
        //    //        //        bool isJishu = (count % 2 == 1) ? true : false;
        //    //        //        if (isJishu)
        //    //        //        {
        //    //        //            Value_Column = dataList[(count / 2) + 1];
        //    //        //        }
        //    //        //        else
        //    //        //        {
        //    //        //            double val1 = dataList[(count / 2)];
        //    //        //            double val2 = dataList[(count / 2) + 1];
        //    //        //            Value_Column = (val1 + val2) / 2;
        //    //        //        }
        //    //        //    }
        //    //        //    else if (dataList.Count == 1)
        //    //        //    {
        //    //        //        Value_Column = dataList[0];
        //    //        //    }

        //    //        //    break;
        //    //    }
        //    //}
        //    #endregion
           
        //    //// Finalization. ?
        //    if (e.SummaryProcess == CustomSummaryProcess.Finalize)
        //    {
        //        List<double> dataList = SummaryCalculate.GreateDataList(view, column);
        //        switch (key)
        //        {
        //            case "Count":
        //                Value_Column = SummaryCalculate.GetCountValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "Min":
        //                Value_Column = SummaryCalculate.GetMinValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "Max":
        //                Value_Column = SummaryCalculate.GetMaxValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "Median":
        //                Value_Column = SummaryCalculate.GetMidValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "Sum":
        //                Value_Column = SummaryCalculate.GetSumValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "Avg":
        //                Value_Column = SummaryCalculate.GetAvgValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "Range":
        //                Value_Column = SummaryCalculate.GetRangeValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "Sigma":
        //                Value_Column = SummaryCalculate.GetSigmaValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "S":
        //                Value_Column = SummaryCalculate.GetSValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;

        //            case "CP":
        //                Value_Column = SummaryCalculate.GetCPValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "CA":
        //                GridColumn  columnMax=  view.Columns["Max"];
        //                Value_Column = SummaryCalculate.GetCAValue(dataList,);
        //                e.TotalValue = Value_Column;
        //                break;
        //            case "CPK":
        //                Value_Column = SummaryCalculate.GetCPKValue(dataList);
        //                e.TotalValue = Value_Column;
        //                break;
        //        }
        //    }
        //}

        #endregion

    }
}